﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation;

namespace PowerShell.Host.PsTFS.CdmLet.PsObject
{
    public static class PsExtMethode
    {
        public static List<Changeset> ListChangeset(this WorkItem wi) 
        {  
            VersionControlServer vcs = PsService.VCS();
            List<Changeset> associatedChangesets = new List<Changeset>();
            foreach (Link link in wi.Links)
            {
                ExternalLink extLink = link as ExternalLink;
                if (extLink != null)
                {
                    ArtifactId artifact = LinkingUtilities.DecodeUri(extLink.LinkedArtifactUri);
                    if (String.Equals(artifact.ArtifactType, "Changeset", StringComparison.Ordinal))
                    {
                        // Convert the artifact URI to Changeset object.
                        associatedChangesets.Add(vcs.ArtifactProvider.GetChangeset(new Uri(extLink.LinkedArtifactUri)));
                    }
                }
            }

            return associatedChangesets;
        }
    }
}
